package com.longmao.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.longmao.commonutils.R;
import com.longmao.eduservice.entity.EduTeacher;
import com.longmao.eduservice.entity.vo.TeacherQuery;
import com.longmao.eduservice.service.EduTeacherService;
import com.longmao.servicebase.handler.LongMaoException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author longmao
 * @since 2022-01-20
 */
@Api(description="讲师管理")
//@CrossOrigin //跨域
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;

    /**
     * 条件查询分页讲师列表
     * @param page
     * @param limit
     * @param teacherQuery
     * @return
     */
    @ApiOperation(value = "条件查询分页讲师列表")
    @PostMapping("/pageTeacherCondition/{page}/{limit}")
    public R pageTeacherCondition(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "teacherQuery", value = "查询对象")
            @RequestBody(required = false) TeacherQuery teacherQuery){

        Page<EduTeacher> pageParam = new Page<>(page, limit);

        teacherService.pageQuery(pageParam, teacherQuery);
        List<EduTeacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();

        return  R.ok().data("total", total).data("rows", records);
    }

    /**
     * 根据ID查询讲师
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("/getTeacherById/{id}")
    public R getTeacherById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id){

        EduTeacher eduTeacher = teacherService.getById(id);
        return R.ok().data("teacher", eduTeacher);
    }

    /**
     * 删除讲师
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("/removeTeacherById/{id}")
    public R removeTeacherById(@ApiParam(name = "id", value = "讲师id", required = true) @PathVariable String id){
        boolean result = teacherService.removeById(id);
        if(result) {
            return R.ok();
        }else {
            return R.error();
        }
    }

    /**
     * 根据ID修改讲师
     * @param eduTeacher
     * @return
     */
    @ApiOperation(value = "根据ID修改讲师")
    @PostMapping("/updateTeacher")
    public R updateTeacher(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher eduTeacher){

        boolean update = teacherService.updateById(eduTeacher);
        if(update) {
            return R.ok();
        }else {
            return R.error();
        }
    }

    /**
     * 新增讲师
     * @param eduTeacher
     * @return
     */
    @ApiOperation(value = "新增讲师")
    @PostMapping("/saveTeacher")
    public R saveTeacher(
            @ApiParam(name = "eduTeacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher eduTeacher){

        boolean save = teacherService.save(eduTeacher);
        if(save) {
            return R.ok();
        }else {
            return R.error();
        }

    }

    /**
     * 查询所有讲师
     * @return
     */
    @ApiOperation(value = "查询所有讲师")
    @GetMapping("/findAll")
    public R findAll() {
       return R.ok().data("items", teacherService.list(null));
    }
}

